package com.youthburin.rmp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youthburin.base.controller.BaseController;
import com.youthburin.base.services.OpLogService;
import com.youthburin.base.util.DwzPagination;
import com.youthburin.base.util.DwzResponse;
import com.youthburin.base.util.DwzResponseUtils;
import com.youthburin.rmp.domain.RmpDept;
import com.youthburin.rmp.enums.RmpOpLogBusiTypeEnum;
import com.youthburin.rmp.enums.RmpOpLogTypeEnum;
import com.youthburin.rmp.services.RmpDeptService;

@Controller
public class RmpDeptController extends BaseController {
	@Autowired
	private RmpDeptService rmpDeptService;
	@Autowired
	@Qualifier("rmpOpLogService")
	private OpLogService opLogService;

	@RequiresPermissions("dept:list")
	@RequestMapping(value = "/dept/list")
	public String list(HttpServletRequest request) {
		opLogService.addLog(RmpOpLogTypeEnum.LIST.toString(), RmpOpLogBusiTypeEnum.RMP_DEPT.toString());
		rmpDeptService.dwzFindAll((DwzPagination) request.getAttribute(DwzPagination.DWZPAGINATION_PROPERTY_KEY));
		return "dept/list";
	}

	@RequiresPermissions("dept:add")
	@RequestMapping(value = "/dept/add", method = RequestMethod.GET)
	public String toAdd() {
		return "dept/edit";
	}

	@RequiresPermissions("dept:add")
	@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse add(@Valid RmpDept rmpDept, BindingResult bindingResult,
			@RequestParam("parentDept.code") String parentCode) {
		if (bindingResult.hasErrors()) {
			return DwzResponseUtils.createFailResponse(bindingResult.getAllErrors());
		} else {
			rmpDept.setParentCode(parentCode);
			RmpDept newRmpDept = rmpDeptService.save(rmpDept);
			opLogService.addLog(RmpOpLogTypeEnum.ADD.toString(), RmpOpLogBusiTypeEnum.RMP_DEPT.toString(),
					newRmpDept.getCode(), newRmpDept.getName());
			return DwzResponseUtils.createSuccessResponse(true, "dept_panel");
		}
	}

	@RequiresPermissions("dept:edit")
	@RequestMapping(value = "/dept/edit/{code}", method = RequestMethod.GET)
	public String toEdit(@PathVariable("code") String code, ModelMap model) {
		RmpDept dept = rmpDeptService.get(code);
		model.put("dept", dept);
		if (StringUtils.isNotBlank(dept.getParentCode())) {
			model.put("parentName", rmpDeptService.get(dept.getParentCode()).getName());
		}
		return "dept/edit";
	}

	@RequiresPermissions("dept:view")
	@RequestMapping(value = "/dept/view/{code}", method = RequestMethod.GET)
	public String toView(@PathVariable("code") String code, ModelMap model) {
		RmpDept dept = rmpDeptService.get(code);

		opLogService.addLog(RmpOpLogTypeEnum.VIEW.toString(), RmpOpLogBusiTypeEnum.RMP_DEPT.toString(), dept.getCode(),
				dept.getName());
		model.put("dept", dept);
		if (StringUtils.isNotBlank(dept.getParentCode())) {
			model.put("parentName", rmpDeptService.get(dept.getParentCode()).getName());
		}
		model.put("readOnly", "1");
		return "dept/edit";
	}

	@RequiresPermissions("dept:edit")
	@RequestMapping(value = "/dept/edit", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse edit(@Valid RmpDept rmpDept, BindingResult bindingResult,
			@RequestParam("parentDept.code") String parentCode) {

		if (bindingResult.hasErrors()) {
			return DwzResponseUtils.createFailResponse(bindingResult.getAllErrors());
		} else {
			rmpDept.setParentCode(parentCode);
			RmpDept newRmpDept = rmpDeptService.update(rmpDept);

			opLogService.addLog(RmpOpLogTypeEnum.UPDATE.toString(), RmpOpLogBusiTypeEnum.RMP_DEPT.toString(),
					newRmpDept.getCode(), newRmpDept.getName());
			return DwzResponseUtils.createSuccessResponse(true, "dept_panel");
		}
	}

	@RequiresPermissions("dept:delete")
	@RequestMapping(value = "/dept/delete", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse delete(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			List<String> idList = new ArrayList<String>();
			for (String id : ids.split(",")) {
				if (StringUtils.isNotBlank(id)) {
					idList.add(StringUtils.trim(id));
				}
			}
			rmpDeptService.delete(idList);

			for (String id : idList) {
				opLogService.addLog(RmpOpLogTypeEnum.DELETE.toString(), RmpOpLogBusiTypeEnum.RMP_DEPT.toString(), id);
			}
		}
		return DwzResponseUtils.createSuccessResponse(false);
	}

	@RequestMapping(value = "/dept/tree")
	public String tree(ModelMap modelMap) {
		String tree = rmpDeptService.getTreeString("tree treeFolder expand");
		modelMap.put("tree", tree);
		return "dept/tree";
	}
}
